var basePath="/";
var parentParams=artDialog.data('params');
var map;
var starMap;
var marker;
function sendData(params){
  map = params.map;
  marker = params.marker;
  starMap = window.parent.starMap;
}
$(function(){
  $('.close').unbind().click(function(){
    $(this).parent().remove();
  }); 
  $('.dataImg').unbind().click(function(){
    if($('#viewImg').length>0){ 
      $('#viewImg').remove(); 
    } 
    $('body').append('<img id="viewImg" style="display:none" src="'+$(this).attr("src")+'">');
    $('#viewImg').on("load", function(){
      var w=$(this).width();
      var h=$(this).height();
      var l = w / h;
      if(h > handle.getScreenHeight() * 0.85){
        h = handle.getScreenHeight() * 0.85;
        w = h * l;
      }
      artDialog.alert2('<div style="width:'+w+'px;height:'+h+'px;"><img style="height:'+h+'px" src="'+$(this).attr("src")+'">') 
    }); 
  }); 
  $("#editWarInfoForm").validate({
    rules: {
      cateId: {
        required: true
      },
      lng: {
        required: true,
        zhengze: ".{2,20}"
      },
      lat: {
        required: true,
        zhengze: ".{2,20}"
      },
      stationNames:{
        zhengze: "\\s*[A-Za-z0-9_\u4e00-\u9fa5]+\\s*([,，]\\s*[A-Za-z0-9_\u4e00-\u9fa5]+\\s*)*"
      }
    },
    messages: {
      cateId: {
        required: "必选"
      },
      lng: {
        required: "必填",
        zhengze: "长度在2至20个字"
      },
      lat: {
        required: "必填",
        zhengze: "长度在2至20个字"
      },
      stationNames:{
        zhengze: "由汉字字母数字下划线组成，多个用逗号分隔"
      }
    }
  });
  var handle = new UtilsHandle({
    basePath: "/",
    uploadImages:{uploadFileId: 'uploadImage', multiple: true, items: [{
      data: {"mark": 0, "of": 1},
      uploadBtn: $('#uploadImgBtn'), 
      success: function (data, textStatus) {
        if(data.code==0){
          for(var o in data.data){
            var pic = data.data[o].original;
            $('#imgSpan').append('<span style="position: relative;"><img class="dataImg" width="70px" height="70px" src="'+pic+'" data="'+pic+'"><div class="close">X</div></span>'); 
          }
        }else{
          artDialog.alert(data.msg);
        }
      },
      complete: function (XMLHttpRequest, textStatus) {
        $('.close').unbind().click(function(){
          $(this).parent().remove();
        }); 
        $('.dataImg').unbind().click(function(){
          if($('#viewImg').length>0){ 
            $('#viewImg').remove(); 
          } 
          $('body').append('<img id="viewImg" style="display:none" src="'+$(this).attr("src")+'">');
          $('#viewImg').on("load", function(){
            var w=$(this).width();
            var h=$(this).height();
            var l = w / h;
            if(h > handle.getScreenHeight() * 0.85){
              h = handle.getScreenHeight() * 0.85;
              w = h * l;
            }
            artDialog.alert2('<div style="width:'+w+'px;height:'+h+'px;"><img style="height:'+h+'px" src="'+$(this).attr("src")+'">') 
          }); 
        }); 
      } 
    }]
    }
  });

  $("#saveBtn").click(function(){
    var flag = $("#editWarInfoForm").valid();
    if(! flag) return;
    var data=$("#editWarInfoForm").serializeArray();
    var params = {};
    $.each(data, function(i, field){
      var name = field.name;
      params[name] = field.value;
      console.log(name + "  " + field.value)
    });
    var pictures = new Array();
    $("#imgSpan .dataImg").each(function(){
      var path = $(this).attr("data");
      if(path) {
        pictures.push({"url": path});
      }
    });
    params["pictures"] = pictures;
    var infos = {};
    $("tr.infoTr").each(function(){
      var key = $.trim($("strong span", $(this)).text());
      var val = $("input", $(this)).val();
      infos[key] = val;
    });
    params["infos"] = infos;
    if(params.stationNames){
      var stations = params.stationNames.split(/[,，]/);
      var nf = [];
      for(var o in stations){
        var f = stations[o].replace(/\s*/g, "");
        if(nf.indexOf(f) != -1){
          artDialog.alert("监测站【"+f+"】已存在");
          return false;
        }
        nf.push(f);
      }
      params.stations = nf;
    }
    $("#saveBtn").attr("disabled", true);
    $.ajax({
      contentType: "application/json",
      url: basePath+"warInfo/edit",
      data: JSON.stringify(params),
      type: 'post',
      dataType: 'json',
      complete: function(xhr, ts){
        $("#saveBtn").removeAttr("disabled");
      },
      success: function(res){
        if(res.code==0){
          null != starMap ? starMap.getMap().closeInfoWindow() : art.dialog.close();
        }else{
          artDialog.alert(res.msg)
        }
        $("#saveBtn").removeAttr("disabled");
      }
    });
  });
  $("#delBtn").click(function(){
    var id = $(this).attr("data-id");
    artDialog.confirm("确定要删除该记录？", function(){
      $("#delBtn").attr("disabled", true);
      $.ajax({
        url: basePath+"warInfo/deleted",
        data: {"ids": id},
        type: 'post',
        dataType: 'json',
        success: function(res){
          if(res.code==0){
            starMap.getMap().removeOverLay(marker);
            null != starMap ? starMap.getMap().closeInfoWindow() : art.dialog.close();
          }else{
            artDialog.alert(res.msg)
          }
          $("#saveBtn").removeAttr("disabled");
        }
      });
    });
  });
});